package com.exam.controller;


import com.exam.entity.*;
import com.exam.serviceimpl.LoginServiceImpl;
import com.exam.util.ApiResultHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

//@Controller
//@RequestMapping(value = {"/logincontroller"})
@RestController
public class LoginController {

    @Autowired
    private LoginServiceImpl loginService;

    @PostMapping(value = "/login")//形式不一样
    public ApiResult login(@RequestBody Login login){
        Integer username=login.getUsername();
        String password=login.getPassword();

        Admin adminRes =loginService.adminLogin(username,password);
        if(adminRes!=null){
            return ApiResultHandler.buildApiResult(200,"请求成功",adminRes);
        }

        Teacher teacherRes =loginService.teacherLogin(username,password);
        if(teacherRes!=null){
            return ApiResultHandler.buildApiResult(200,"请求成功",teacherRes);
        }

        Student studentRes =loginService.studentLogin(username,password);
        if(studentRes!=null){
            return ApiResultHandler.buildApiResult(200,"请求成功",studentRes);
        }
        return ApiResultHandler.buildApiResult(400,"请求失败",null);
    }
}
